<?php
// +----------------------------------------------------------------------
// | 麦沃德科技赋能开发者，助力中小企业发展
// +----------------------------------------------------------------------
// | Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
// +----------------------------------------------------------------------
// | Wdadmin系统产品软件并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
// +----------------------------------------------------------------------
// | Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
// +----------------------------------------------------------------------
/**
 * Class Map
 * Desc  地图控制器
 * Create on 2024/10/22 9:55
 * Create by wangyafang
 */

namespace app\api\controller\wddj\user;


use app\api\model\wddj\Config;
use app\api\model\wddj\driver\Driver;
use app\api\model\wddj\driver\OperationConfig;
use app\common\controller\Api;

class Map extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['by_location_get_driver','get_driver_routes'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * Desc 通过位置获取代驾列表
     * Create on 2024/10/22 10:25
     * Create by wangyafang
     */
    public function by_location_get_driver()
    {
        if (!$this->request->isGet()) {
            $this->error('请求类型错误');
        }


        // 获取经度和纬度并进行验证
        $lng = $this->request->get('lng');
        $lat = $this->request->get('lat');

        // 验证经度和纬度是否为空
        if (empty($lng)) {
            $this->error('请输入经度');
        }
        if (empty($lat)) {
            $this->error('请输入纬度');
        }

        // 过滤并转换为浮动数值
        $lng = floatval($lng);
        $lat = floatval($lat);

        // 验证经度和纬度是否在有效范围内
        if ($lng < -180 || $lng > 180) {
            $this->error('经度值不在有效范围内');
        }
        if ($lat < -90 || $lat > 90) {
            $this->error('纬度值不在有效范围内');
        }

        $earthRadius = 6371;
        $radius = (new OperationConfig())->where('id', 1)->value('user_kilometer'); // 获取用户距离范围

        $prefix = config('database.prefix');

        // 构建原生 SQL 查询
        $sql = "SELECT id, driver_lng, driver_lat, 
                (" . $earthRadius . " * ACOS(
                    COS(RADIANS($lat)) * COS(RADIANS(driver_lat)) * 
                    COS(RADIANS(driver_lng) - RADIANS($lng)) + 
                    SIN(RADIANS($lat)) * SIN(RADIANS(driver_lat))
                )) AS distance
            FROM ".$prefix."wddj_driver
            HAVING distance < $radius
            ORDER BY distance ASC";

        // 执行原生查询
        $data = (new Driver())->query($sql);

        $this->success('请求成功', $data);
    }



    /**
     * Desc 驾车（driving）路线规划
     * Create on 2024/11/1 9:28
     * Create by wangyafang
     */
    public function get_driver_routes($param)
    {
        $from_lat = $param['from_lat'];//出发地纬度
        $from_lng = $param['from_lng'];
        $to_lat = $param['to_lat'];
        $to_lng = $param['to_lng'];

        $from_lat = floatval($from_lat);
        $from_lng = floatval($from_lng);
        $to_lat = floatval($to_lat);
        $to_lng = floatval($to_lng);

        // 验证经度和纬度是否在有效范围内
        if ($from_lng < -180 || $from_lng > 180) {
            $this->error('经度值不在有效范围内');
        }
        if ($from_lat < -90 || $from_lat > 90) {
            $this->error('纬度值不在有效范围内');
        }

        if ($to_lng < -180 || $to_lng > 180) {
            $this->error('经度值不在有效范围内');
        }
        if ($to_lat < -90 || $to_lat > 90) {
            $this->error('纬度值不在有效范围内');
        }

        $key = (new Config())->where('id',1)->value('tencent_map_web_service_key');
        $url = "https://apis.map.qq.com/ws/direction/v1/driving/?from=$from_lat,$from_lng&to=$to_lat,$to_lng&output=json&callback=cb&key=$key";
        $result = wddj_curl_get_https($url);
        $mapResult = json_decode($result,true);
        if ($mapResult['status'] != 0) {
            $this->error('腾讯地图错误提示：'.$mapResult['message']);
        }
        return $mapResult;
    }


}



 